package com.shop.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.pojo.pms.Pms_Brand;
import com.shop.service.Pms_Brand_Service;
import com.shop.utils.LayuiUtils;
import com.shop.utils.R;
import com.shop.valid.AddGroup;
import com.shop.valid.UpGroup;
import com.shop.valid.UpStatusGroup;
import com.shop.vo.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.awt.font.ImageGraphicAttribute;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/brand")
public class Pms_Brand_Controller {
    @Autowired
    private Pms_Brand_Service pms_brand_service;

    //根据商品分类id去查询当前分类下的全部品牌信息
    @GetMapping("/findAllBrandByCatalogId/{catelogId}")
    public R findAllBrandByCatalogId(@PathVariable Integer catelogId){

        return pms_brand_service.findAllBrandByCatalogId(catelogId);
    }

    /**
     * 后台查询所有的品牌信息
     * @param page
     * @param limit
     * @param pms_brand
     * @return
     */
    @GetMapping("/find_admin_Brand_All")
    public LayuiUtils<Pms_Brand> find_admin_Brand_All(Integer page, Integer limit, Pms_Brand pms_brand){

        System.out.println(page+"===="+limit);
        Page<Pms_Brand> pages = new Page<>(page, limit);

        return pms_brand_service.find_admin_Brand_All(pages,pms_brand);
    }

    @PostMapping("/insert_admin_All")
    public R insert_admin_All(@RequestBody @Validated({AddGroup.class}) Pms_Brand pms_brand) {
        System.out.println(pms_brand);
//        if (bindingResult.hasErrors()) {
//            Map<String, Object> map = new HashMap<>();
//            //获取校验的结果
//            bindingResult.getFieldErrors().forEach((item) -> {
//                //获取自定义错误的提示
//                String defaultMessage = item.getDefaultMessage();
//                //获取错误的属性名
//                String field = item.getField();
//                map.put(field, defaultMessage);
//
//            });

//            return R.error(500,"数据不合格").put("data",map);
//        }
        return pms_brand_service.insert_admin_All(pms_brand);
    }
    /**
     * 删除品牌信息
     * @param brand_id
     * @return
     */
    @DeleteMapping("/delete_admin_All")
    public R delete_admin_All(@RequestParam List<String> brand_id){
        System.out.println(brand_id);
        return  pms_brand_service.delete_admin_All(brand_id);

    }

    /**
     * 后台修改品牌方法
     * @param pms_brand
     * @return
     */
    @PutMapping("/up_admin_All")
    public R up_admin_All(@RequestBody @Validated({UpGroup.class}) Pms_Brand pms_brand){

        return pms_brand_service.up_admin_All(pms_brand);
    }

    /**
     * 后台修改品牌显示状态
     * @param pms_brand
     * @return
     */
    @PutMapping("/up_admin_status_All")
    public R up_admin_status_All(@RequestBody @Validated({UpStatusGroup.class}) Pms_Brand pms_brand){

        return pms_brand_service.up_admin_All(pms_brand);
    }


    @RequestMapping("/filesUploadLayui")
    //requestParam要写才知道是前台的那个数组
    public String filesUploadLayui(MultipartFile file,HttpServletRequest request) throws IllegalStateException, IOException {
        String imgUrl = "";
        //这里的files 每次都只有一个文件，因为layui上传的时候文件是一个一个上传的
        if (file != null) {
            // 保存文件
            imgUrl = OssUtil.uploadImg(file);//上传到OSS，返回上传的图片地址，访问该地址就可以看见图片
        }

        imgUrl=imgUrl.replace("%2F","/");
        imgUrl=imgUrl.replace("\\","/");
        //   System.out.println(imgUrl);

        JSONObject  map = new JSONObject();
        map.put("code",0);
        map.put("msg","上传成功");
        map.put("data", imgUrl);

        return map.toJSONString();
    }



    //多文件上传，利用单文件上传修改而来
    @RequestMapping("/filesUpload")
    //requestParam要写才知道是前台的那个数组
    public String filesUpload(MultipartFile file,HttpServletRequest request) throws IllegalStateException, IOException {
        String imgUrl = "";
        //这里的files 每次都只有一个文件，因为layui上传的时候文件是一个一个上传的
        if (file != null) {
                // 保存文件
                imgUrl = OssUtil.uploadImg(file);//上传到OSS，返回上传的图片地址，访问该地址就可以看见图片
        }

        imgUrl=imgUrl.replace("%2F","/");
        imgUrl=imgUrl.replace("\\","/");
     //   System.out.println(imgUrl);
        return imgUrl;
    }

    /*
     * @Description 删除图片
     * @param objectName
     * @return void
     * @author liusupeng
     * @date 2020/11/8 14:32
     */
    @DeleteMapping("/deletefiles")
    public void deleteFile( String objectName){

        System.out.println(objectName);
        OssUtil.deleteImg(objectName);

    }

}
